package dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import common.CommonUtil;

import vo.Customer;
import vo.User;
import dao.UserDao;
@Repository
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
	@Resource
	private SessionFactory sessionFactory;
	
	 @Autowired  
	 public void setSessionFactoryOverride(SessionFactory sessionFactory) 
	    {   
	      super.setSessionFactory(sessionFactory);   
	    }    

	@SuppressWarnings("unchecked")
	public List<User> findAllUserser() {

//		String hql = "from User user order by user.id desc";
//
//		List<User> list = this.getHibernateTemplate().find(hql);

		return new ArrayList<User>();

	}
	@SuppressWarnings("unchecked")
	public ArrayList<User> findUserByArgus(String hql,Object[] params) {

		ArrayList<User> list = (ArrayList<User>) this.getHibernateTemplate().find(hql, params);

		return  new ArrayList<User>();
	}
	@SuppressWarnings("unchecked")
	public void removeUser(User user) {

//		this.getHibernateTemplate().delete(user);

	}
	@SuppressWarnings("unchecked")
	public void saveUser(User user) {
		System.out.println(user);
		sessionFactory.getCurrentSession().save(user);
		
		for (int i=0; i<500; i++) {
			User newUser = new User();
			newUser.setAccount(user.getAccount() + i);
			newUser.setName(user.getName() + i);
			newUser.setPassword(user.getPassword() + i);
			newUser.setName_py(user.getName_py() + i);
			newUser.setReg_time(CommonUtil.getDate());
			newUser.setSex(user.getSex());
			newUser.setEmail(user.getEmail() + i);
			newUser.setNative_place(user.getNative_place() + i);
			newUser.setIs_receive_email(user.getIs_receive_email());
			
			sessionFactory.getCurrentSession().save(newUser);
		}

	}
	@SuppressWarnings("unchecked")
	public void updateUser(User user) {

//		this.getHibernateTemplate().update(user);

	}
	@SuppressWarnings("unchecked")
	public User findUserByName(String account) {
		User user = (User)this.getHibernateTemplate().get(User.class,account);
		return user;
//		Session session = sessionFactory.openSession();
//		Query query = session.createQuery("from vo.User where name = '" + name + "'");
//		System.out.println(query.getQueryString());
//		List<User> list=query.list();
//		System.out.println("user-size:"+list.size());	
//		
//		if(list.size()==1)
//	      return list.get(0);
//		else 
//		  return new User();
	}


	public List<Object> search(String hql, Object[] params, int start, int length) {
		// TODO 自动生成方法存根
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		query.setFirstResult(start);
		query.setMaxResults(length);
		
		return (List<Object>)query.list();
	}

	public Long count(String name) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("select count(*) from vo.User where name like '%" + name + "%'");
		Long count = (Long)query.list().get(0);
		
		return count;
	}

}
